//    Copyright (C) Kherty.  All rights reserved.
using System.Windows.Media;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet
{
    ///<summary>
    /// Base class for all Spreadsheet colors.
    ///</summary>
    public abstract class SpreadsheetColor
    {
        public override abstract int GetHashCode();
        public abstract override bool Equals(object obj);
        public static SpreadsheetColor ReadColor(string name, ReadContext context)
        {
            using (context.Read(name))
            {
                return ReadColor(context);
            }

        }

        static SpreadsheetColor ReadColor(ReadContext c)
        {
            var workbook = c.GetData<Workbook>();
            {
                bool? auto = c.GetOptionalBoolean("auto");
                int? Indexed = c.GetOptionalInteger("indexed");
                string Rgb = c.GetOptionalString("rgb");
                int? Theme = c.GetOptionalInteger("theme");
                double? Tint = c.GetOptionalDouble("tint");
                if (auto != null)
                {
                    return new AutomaticColor(c.GetData<FillMarker>() != null);
                }
                if (Rgb != null)
                {
                    return new RgbColor((Color)ColorConverter.ConvertFromString("#" + Rgb));
                }
                if (Indexed != null)
                {
                    return new IndexedColor(workbook.StyleSheet.GetIndexedColor(Indexed.Value), Indexed.Value);
                    //\\throw new NotImplementedException();
                }
                return new ThemeColor((ColorSchemeIndex)Theme.Value, Tint);
            }

        }
        public static SpreadsheetColor ReadOptionalColor(ReadContext c)
        {
            Workbook workbook = c.GetData<Workbook>();
            {
                bool? auto = c.GetOptionalBoolean("auto");
                int? Indexed = c.GetOptionalInteger("indexed");
                string Rgb = c.GetOptionalString("rgb");
                int? Theme = c.GetOptionalInteger("theme");
                double? Tint = c.GetOptionalDouble("tint");
                if (auto != null)
                {
                    return new AutomaticColor(c.GetData<FillMarker>() != null);
                }
                if (Rgb != null)
                {
                    return new RgbColor((Color)ColorConverter.ConvertFromString("#" + Rgb));
                }
                if (Indexed != null)
                {
                    return new IndexedColor(workbook.StyleSheet.GetIndexedColor(Indexed.Value), Indexed.Value);
                    //\\throw new NotImplementedException();
                }
                if (Theme != null)
                    return new ThemeColor((ColorSchemeIndex)Theme.Value, Tint);
                return null;
            }

        }

        internal abstract void WriteXmlAs(string name, WriteContext context);


        public static implicit operator SpreadsheetColor(Color color)
        {
            return new RgbColor(color);
        }

        public abstract Color GetColor(ColorScheme colorScheme);
    }
}